
class ProductAttachmentUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  process :store_dimensions
  process :resize_to_limit => [1200, 1200]

  def default_resize_url
    self.file.url
    #if !self.model.width || self.model.width < 480
      #self.file.url
    #else
      #"#{ENV['IMAGE_RESIZE_HOST']}/#{self.file.path}"
    #end
  end

  def store_dir
    "products/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  def store_dimensions
    if file && model
      begin
        model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
      rescue
        #ignore
      end
    end
  end

end

